﻿Imports System.Xml
Imports System.Xml.Serialization

Public Class Map
	<XmlAttribute("ID")> Public MapID As String = ""

	<XmlAttribute("Width")> Public Width As Integer
	<XmlAttribute("Height")> Public Height As Integer

	<XmlAttribute("StartX")> Public StartX As Integer = 0
	<XmlAttribute("StartY")> Public StartY As Integer = 0

	<XmlIgnore> Public Graphics As Graphics
	<XmlIgnore> Public BitMap As Bitmap

	<XmlIgnore> Public PixelWidth As Integer
	<XmlIgnore> Public PixelHeight As Integer

	<XmlElement("TopMap")> Public MapTopID As String = ""
	<XmlElement("BottomMap")> Public MapBottomID As String = ""
	<XmlElement("LeftMap")> Public MapLeftID As String = ""
	<XmlElement("RightMap")> Public MapRightID As String = ""

	' Matrix of tiles (array of arrays)
	Public MapTileMatrix As New List(Of List(Of MapTile))
	<XmlIgnore> Public PauseUpdate As Boolean = False

	Public Sub New()

	End Sub

	Public Sub New(width As Integer, height As Integer)
		Me.Width = width
		Me.Height = height
		'ReDim MapTileMatrix(Me.Width - 1, Me.Height - 1)
		For x As Integer = 0 To Me.Width - 1
			MapTileMatrix.Add(New List(Of MapTile))
			For y As Integer = 0 To Me.Height - 1
				MapTileMatrix(x).Add(New MapTile())
			Next
		Next
		PixelWidth = (Me.Width * MapTile.Width)
		PixelHeight = (Me.Height * MapTile.Height)
		BitMap = New Bitmap(PixelWidth, PixelHeight)
		BitMap.MakeTransparent(Color.Transparent)
		Graphics = Drawing.Graphics.FromImage(BitMap)
	End Sub

	Public Sub FinishLoad()
		PixelWidth = Me.Width * MapTile.Width
		PixelHeight = Me.Height * MapTile.Height
		BitMap = New Bitmap(PixelWidth, PixelHeight)
		BitMap.MakeTransparent(Color.Transparent)
		Graphics = Drawing.Graphics.FromImage(BitMap)

		For x As Integer = 0 To Me.Width - 1
			For y As Integer = 0 To Me.Height - 1
				MapTileMatrix(x)(y).FinishLoad()
			Next
		Next
	End Sub



	Public Function Update()
		If Not PauseUpdate Then
			For x As Integer = 0 To Width - 1
				For y As Integer = 0 To Height - 1
					'BitMap.SetPixel(x, y, MapDataMatrix(x, y).Color)
					Dim startX As Integer = x * MapTile.Width
					Dim startY As Integer = y * MapTile.Height

					' Draws each tile to main bitmap using tile's bitmap and at location startX,startY
					Graphics.DrawImage(MapTileMatrix(x)(y).BitMap, startX, startY)

					'For x1 = 0 To MapData.Width - 1
					'	For y1 = 0 To MapData.Height - 1
					'		BitMap.SetPixel(startX + x1, startY + y1, MapDataMatrix(x, y).BitMap.GetPixel(x1, y1))
					'	Next
					'Next
				Next
			Next
		End If
		Return BitMap
	End Function
End Class
